package org.dearsoft.keepleaping.seam.example.dvdstore.accountdeposits;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.FlushModeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.bpm.CreateProcess;

import com.jboss.dvd.seam.Customer;

@Stateful
@Name("accountDeposits")
@Scope(ScopeType.CONVERSATION)
public class DepositAction implements Deposit, Serializable {
	
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -2937853331601163516L;

	private BigDecimal money = BigDecimal.ZERO;

	/* (non-Javadoc)
	 * @see org.dearsoft.keepleaping.seam.example.dvdstore.accountdeposits.Deposit#setMoney(java.math.BigDecimal)
	 */
	public void setMoney(BigDecimal money) {
		this.money = money;
	}

	/* (non-Javadoc)
	 * @see org.dearsoft.keepleaping.seam.example.dvdstore.accountdeposits.Deposit#getMoney()
	 */
	public BigDecimal getMoney() {
		return money;
	}
	
	@In(value = "accountDepositsRecord")
	@Out(value = "confirmedDepositsRecord")
	private Record record;
	
	@In("currentUser")
	private Customer customer;

	@PersistenceContext(type = PersistenceContextType.EXTENDED)
	private EntityManager em;

	@Out(required = false,scope = ScopeType.BUSINESS_PROCESS)
	private String customerName;

	@Out(required = false,scope = ScopeType.BUSINESS_PROCESS)
	private BigDecimal depositAmount;

	@Out(required = false,scope = ScopeType.BUSINESS_PROCESS)
	private long recordId;

	
	/* (non-Javadoc)
	 * @see org.dearsoft.keepleaping.seam.example.dvdstore.accountdeposits.Deposit#deposit()
	 */
//	@Begin(flushMode = FlushModeType.MANUAL,pageflow = "accountDeposits-pageflow")
	public void deposit() {
		record.setCustomer(customer);
		record.setDepositAmount(money);
		record.setDepositTime(new Date());
	}
	
	/* (non-Javadoc)
	 * @see org.dearsoft.keepleaping.seam.example.dvdstore.accountdeposits.Deposit#end()
	 */
	@End(beforeRedirect = true)
	public void end() {
		// nothing need to be done here
	}
	
	/* (non-Javadoc)
	 * @see org.dearsoft.keepleaping.seam.example.dvdstore.accountdeposits.Deposit#createRecord()
	 */
	@End
	@CreateProcess(definition = "AccountDeposits", processKey = "#{accountDepositsRecord.id}")
	public void createRecord() {	
		em.persist(record);
		em.flush();
		recordId = record.getId(); 
		customerName = record.getCustomer().getUserName();
		depositAmount = record.getDepositAmount(); 
	}
	


	
	/* (non-Javadoc)
	 * @see org.dearsoft.keepleaping.seam.example.dvdstore.accountdeposits.Deposit#destroy()
	 */
	@Remove
	public void destroy() {	
	}

	@Begin(pageflow = "accountDeposits-pageflow",join = true, flushMode = FlushModeType.MANUAL)
	public void begin() {
		// TODO Auto-generated method stub
		
	}
	
	
	
}
